<?php
class omeanalysts_mdl_ome_omsax extends dbeav_model{

    //var $has_export_cnf = true;

    var $export_name = 'OMS未过数';

    public function table_name($real=false){
        $table_name = 'ome_delivery';
        if($real){
            return kernel::database()->prefix.$table_name;
        }else{
            return $table_name;
        }
    }

    public function count($filter=null){

        $where = $this->_filter($filter); 

        $sql = "SELECT  count(*) as _count from  sdb_wms_deliverycomp a 
inner join sdb_wms_deliverycomp_line b on a.id = b.deliverycomp_id
inner join sdb_wms_deliverynote_line d on b.deliverynote_id = d.deliverynote_id
inner join sdb_wms_delivery e on d.delivery_bn = e.delivery_bn
inner join sdb_wms_delivery_items items on e.delivery_id=items.delivery_id
                     where ".$where." ";

        $row = $this->db->select($sql);
        return intval($row[0]['_count']);
    }

    public function getlist($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
  

        if(!$filter['time_from'] && !$filter['time_to']){
            return false;
        }else{

            $where = $this->_filter($filter); 
          $sql = "SELECT FROM_UNIXTIME(a.deliverycomp_time, '%Y-%m-%d') as deliverycomp_time,d.ax_name,d.ax_shopname,d.axorderno,d.axshipno,items.bn,items.product_name,sum(items.number) number from  sdb_wms_deliverycomp a 
inner join sdb_wms_deliverycomp_line b on a.id = b.deliverycomp_id
inner join sdb_wms_deliverynote_line d on b.deliverynote_id = d.deliverynote_id
inner join sdb_wms_delivery e on d.delivery_bn = e.delivery_bn
inner join sdb_wms_delivery_items items on e.delivery_id=items.delivery_id
                  where ".$where."

group by FROM_UNIXTIME(a.deliverycomp_time, '%Y-%m-%d'),d.ax_name,d.ax_shopname,d.axorderno,d.axshipno,items.bn,items.product_name ";
            $rows = $this->db->selectLimit($sql,$limit,$offset);
          
            var_dump(json_encode($rows));
            return $rows;
        }
        
    }
    
    public function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(
            'reship_bn'=>app::get('base')->_('订单号'),
        );
        return $Options = array_merge($parentOptions,$childOptions);
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null){


        $where = array(1);
          $where[]=' a.status="unconfirmed" ';
       if(empty($filter['time_from']))
        {
          $where[]=' 1=2 ';
        }
        if(isset($filter['time_from']) && $filter['time_from']){
            $where[] = ' a.deliverycomp_time >='.strtotime($filter['time_from']);
        }
        if(isset($filter['time_to']) && $filter['time_to']){
            $where[] = ' a.deliverycomp_time <'.(strtotime($filter['time_to'])+86399);
        }

        if(isset($filter['p_bn']) && $filter['p_bn']){
            $where[] = ' b.bn =\''.addslashes($filter['p_bn']).'\'';
        }
        if(isset($filter['axorderno']) && $filter['axorderno']){
            $where[] = ' d.axorderno =\''.addslashes($filter['axorderno']).'\'';
        }
          
        return implode($where,' AND ');
    }

    public function get_schema(){
        $schema = array (
            'columns' => array (
                'deliverycomp_time' =>
                array (
                  'type' => 'varchar(32)',
                  'label' => 'AX过数日期',
                  'comment' => 'AX过数日期',
                  'editable' => false,
                  'in_list' => true,
                ),
                'ax_name' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '客户',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'ax_shopname' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '客户名称',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'axorderno' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => 'AX订单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ),
                'axshipno' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => 'AX送货单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'bn' =>
                array (
                  'type' => 'varchar(30)',
                  'label' => '物料编码',
                  'editable' => false,
                  'is_title' => true,
                ),
                'product_name'=>
                  array(
                    'type'=>'varchar(32)',
                    'label'=>'物料名称',
                    'in_list' => true,
                    'default_in_list' => true,
                  'width' => '150',
                ),
                'number' =>
                array (
                  'type' => 'number',
                  'label' => '数量汇总',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                  'width' => '50',
                ),
            ),
            'idColumn' => 'name',
            'in_list' => array (
                0 => 'deliverycomp_time',
                1 => 'ax_name',
                2 => 'ax_shopname',
                3 => 'axorderno',
                4 => 'axshipno',
                5 => 'bn',
                6 => 'product_name',
                7 => 'number',        
            ),
            'default_in_list' => array (
                0 => 'deliverycomp_time',
                1 => 'ax_name',
                2 => 'ax_shopname',
                3 => 'axorderno',
                4 => 'axshipno',
                5 => 'bn',
                6 => 'product_name',
                7 => 'number', 
            ),
        );
        return $schema;
    }

    /**
     * 获得日志类型(non-PHPdoc)
     * @see dbeav_model::getLogType()
     */
    public function getLogType($logParams) {
        $type = $logParams['type'];
        $logType = 'none';
        if ($type == 'export') {
            $logType = $this->exportLogType($logParams);
        }
        elseif ($type == 'import') {
            $logType = $this->importLogType($logParams);
        }
        return $logType;
    }
    /**
     * 导出日志类型
     * @param Array $logParams 日志参数
     */
    public function exportLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_export';
        return $type;
    }
    /**
     * 导入操作日志类型
     * @param Array $logParams 日志参数
     */
    public function importLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_import';
        return $type;
    }

    function io_title( $filter, $ioType='csv' )
    {
        switch( $filter ){
            case 'delivery':
                $this->oSchema['csv'][$filter] = array(
                    '*:AX过数日期' => 'deliverycomp_time',
                    '*:客户' => 'ax_name',
                    '*:客户名称' => 'ax_shopname',
                    '*:AX订单号' => 'axorderno',
                    '*:AX发货单' => 'axshipno',
                    '*:物料' => 'bn',
                    '*:物料名称' => 'product_name',
                    '*:数量' => 'number',
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys( $this->oSchema[$ioType][$filter] );
        return $this->ioTitle[$ioType][$filter];
    }

    //根据查询条件获取导出数据
    public function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end){
          //根据选择的字段定义导出的第一行标题
          if($curr_sheet == 1){

              $title = array();
              foreach($this->io_title('delivery') as $k => $v ){
                  $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
              }

              $data['content']['main'][0] = '"'.implode('","',$title).'"';
          }

          $where = $this->_filter($filter);
         
          $sql = "SELECT FROM_UNIXTIME(a.deliverycomp_time, '%Y-%m-%d') as deliverycomp_time,d.ax_name,d.ax_shopname,d.axorderno,d.axshipno,items.bn,items.product_name,sum(items.number) number from  sdb_wms_deliverycomp a 
inner join sdb_wms_deliverycomp_line b on a.id = b.deliverycomp_id
inner join sdb_wms_deliverynote_line d on b.deliverynote_id = d.deliverynote_id
inner join sdb_wms_delivery e on d.delivery_bn = e.delivery_bn
inner join sdb_wms_delivery_items items on e.delivery_id=items.delivery_id
                  where ".$where."

group by FROM_UNIXTIME(a.deliverycomp_time, '%Y-%m-%d'),d.ax_name,d.ax_shopname,d.axorderno,d.axshipno,items.bn,items.product_name ";

            $omedelivery = $this->db->selectLimit($sql,$end,$start);
        

            foreach ($omedelivery as $k => $aFilter) {
                $exptmp_data = array();
                foreach ($aFilter as $col => $val) {
                    $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                    $exptmp_data[] = $aFilter[$col];
                }
                $data['content']['main'][] = implode(',', $exptmp_data);
            }
            return $data;
        
    }
}
